﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace DevSupport.Filters
{
    public class ResourceFilterAttribute : Attribute, IAsyncResourceFilter
    {
        private static Dictionary<string, object> cacheDic = new Dictionary<string, object>();
        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            //Console.WriteLine("CustomerResourceFilterAttribute.OnResourceExecutionAsync Before");

            var path = context.HttpContext.Request.Path;
            if (cacheDic.ContainsKey(path))
            {
                context.Result = (IActionResult)cacheDic[path];
            }
            else
            {
                ResourceExecutedContext resource = await next.Invoke();
                cacheDic[path] = resource.Result; // 这句话的执行就是去执行控制器的构造函数+Action
                //Console.WriteLine("CustomerResourceFilterAttribute.OnResourceExecutionAsync After");
            }


        }
    }
}
